package com.example.oauth2.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.util.StringUtils;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.IOException;
import java.util.*;

/**
 * @author wangbin
 */
@Entity
@Data
@NoArgsConstructor
public class Client implements ClientDetails {

    @Id
    @GenericGenerator(name = "uuid", strategy = "uuid")
    @GeneratedValue(generator = "uuid")
    @Column(length = 32)
    private String id;

    @Column(unique = true, nullable = false)
    private String clientId;

    private String clientSecret;

    private String resourceIds;

    private String scope;

    private String authorizedGrantTypes;

    private String registeredRedirectUri;

    private String authorities;

    private Integer accessTokenValiditySeconds;

    private Integer refreshTokenValiditySeconds;

    private String autoApproveScope;

    private String additionalInformation;

    @Override
    public String getClientId() {
        return this.clientId;
    }

    @Override
    public Set<String> getResourceIds() {
        if (StringUtils.isEmpty(this.resourceIds)) {
            return new HashSet<>();
        } else {
            return StringUtils.commaDelimitedListToSet(this.resourceIds);
        }
    }

    @Override
    public boolean isSecretRequired() {
        return !StringUtils.isEmpty(this.clientSecret);
    }

    @Override
    public String getClientSecret() {
        return this.clientSecret;
    }

    @Override
    public boolean isScoped() {
        return this.getScope().size() > 0;
    }

    @Override
    public Set<String> getScope() {
        if (StringUtils.isEmpty(this.scope)) {
            return new HashSet<>();
        } else {
            return StringUtils.commaDelimitedListToSet(this.scope);
        }
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        if (StringUtils.isEmpty(this.authorizedGrantTypes)) {
            return new HashSet<>();
        } else {
            return StringUtils.commaDelimitedListToSet(this.authorizedGrantTypes);
        }
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        if (StringUtils.isEmpty(this.registeredRedirectUri)) {
            return new HashSet<>();
        } else {
            return StringUtils.commaDelimitedListToSet(this.registeredRedirectUri);
        }
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        if (StringUtils.isEmpty(this.authorities)) {
            return new HashSet<>();
        } else {
            Set<String> authStringSet = StringUtils.commaDelimitedListToSet(this.authorities);
            Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
            authStringSet.forEach(authority -> grantedAuthorities.add(() -> authority));
            return grantedAuthorities;
        }
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return this.accessTokenValiditySeconds;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return this.refreshTokenValiditySeconds;
    }

    @Override
    public boolean isAutoApprove(String scope) {
        return this.getAutoApproveScope().contains(scope);
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        ObjectMapper om = new ObjectMapper();
        Map<String, Object> result = null;
        try {
            result = om.readValue(this.additionalInformation, HashMap.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public Set<String> getAutoApproveScope() {
        if (StringUtils.isEmpty(this.autoApproveScope)) {
            return new HashSet<>();
        } else {
            return StringUtils.commaDelimitedListToSet(this.autoApproveScope);
        }
    }
}
